FROM node:22-alpine AS builder

WORKDIR /app

# 配置国内镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 安装 pnpm 并配置国内镜像源
RUN npm install -g pnpm
RUN pnpm config set registry https://registry.npmmirror.com

# 复制依赖文件以利用Docker层缓存
COPY package.json pnpm-lock.yaml ./

# 安装依赖
RUN pnpm install --frozen-lockfile

# 复制源代码
COPY . .

# 构建应用
RUN pnpm run build



# 第二阶段：使用Nginx部署

FROM nginx:alpine

# 配置apk包管理器使用国内镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

COPY nginx.conf /etc/nginx/nginx.conf

# remove default server config to avoid conflicts
RUN rm -f /etc/nginx/conf.d/default.conf || true

# 从builder阶段复制构建结果
COPY --from=builder /app/dist/ /usr/share/nginx/html/

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]